﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Server.Json
{
    public class JsonHelper
    {
        public static string ServerRequest_serialization(string _sid,string _tab,string _ret, string[] _arr)
        {
            ServerRequest one = new ServerRequest() { sid = _sid,tab=_tab, ret = _ret, arr = _arr };

            return JsonConvert.SerializeObject(one);
        }
        public static string ServerRequest_serialization(string _sid, string _tab, string _ret)
        {
            ServerRequest one = new ServerRequest() { sid = _sid, tab = _tab, ret = _ret };

            return JsonConvert.SerializeObject(one);
        }
        public static string[][] ServerRequest_deserialization(string json)
        {
			try
			{

                string[][] list = new string[4][];

                JObject strTmp = AnalayJsonStringToObj(json);
                list[0] = new string[] { strTmp["sid"].ToString() };
                list[1] = new string[] { strTmp["tab"].ToString() };
                list[2] = new string[] { strTmp["ret"].ToString() };

                if (strTmp["arr"] == null)
                {
                    list[3] = new string[1]; 
                    return list;
                }
                List<string> str = new List<string>();
                foreach (var item in strTmp["arr"].ToArray())
                {
                    str.Add(item.ToString());
                }
                list[3] = str.ToArray();

                return list;
            }
			catch (Exception)
			{

                string[][] list = new string[4][];

                list[0] = new string[] { "error" };
                list[1] = new string[] { "error" };
                list[2] = new string[] { "error" };
                list[3] = new string[] { "error"};
                return list;

            }
        }
        public static string ClientRequest_serialization(string _uid, string _Otype, string _operate, string _text)
        {
            ClientRequest one = new ClientRequest() { uid = _uid, otype = _Otype, operate = _operate, text = _text };

            return JsonConvert.SerializeObject(one);
        }
        public static string[] ClientRequest_deserialization(string json)
        {
            string[] list = new string[4];

            JObject strTmp = AnalayJsonStringToObj(json);
            list[0] = strTmp["uid"].ToString();
            list[1] = strTmp["otype"].ToString();
            list[2] = strTmp["operate"].ToString();
            list[3] = strTmp["text"].ToString();

            return list;
        }
        private static JObject AnalayJsonStringToObj(string jsonStr)

        {

            if (!string.IsNullOrEmpty(jsonStr))

            {

                string strJsonIndex = string.Empty;

                JObject jo = (JObject)JsonConvert.DeserializeObject(jsonStr);

                return jo;

            }

            else

            {

                return null;

            }

        }


    }
}
